package com.jufa_3.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jufa_3.common.R;
import com.jufa_3.dto.SelRetInfo;
import com.jufa_3.dto.SelectInfo;
import com.jufa_3.entity.PayOrder;
import com.jufa_3.service.ComboOrderService;
import com.jufa_3.service.PayOrderService;
import com.jufa_3.service.SelectInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.jufa_3.common.R.success;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/select")
public class SelectController {

    @Autowired
    private SelectInterface selectInterface;

    @Autowired
    private ComboOrderService comboOrderService;

    @Autowired
    private PayOrderService payOrderService;

    /**
     * 套餐扣费查询——个人案件基础信息核验
     *
     * @param sif
     * @return
     */
    @GetMapping("/getGrajInfoByCombo")
    public R<JSONObject> getGrajInfoByCombo(SelectInfo sif) {
        // 套餐扣费添加收费记录
        sif.setKeyWord("GRSSJL&" + sif.getName() + "&" + sif.getCardNum());
        Integer debit = comboOrderService.debit(sif);
        if (debit == -1) {
            return R.error(4008, "余额不足！");
        }
        sif.setId(debit.toString());
        JSONObject zewubeidiao = selectInterface.zewuGrajjcxxhy(sif);
        if (zewubeidiao == null) {
            return R.error(1004, "程序出错！");
        } else if (zewubeidiao.getInteger("code") == 200) {
            if (zewubeidiao.getInteger("resultCode") == 1) {
                return success(zewubeidiao.getJSONArray("detail").getJSONObject(0), zewubeidiao.getString("messge"));
            } else {
                return R.error(1004, zewubeidiao.getString("messge"));
            }
        } else {
            return R.error(1004, zewubeidiao.getString("messge"));
        }
    }

    /**
     * 支付查询——个人案件基础信息核验
     *
     * @param sif
     * @return
     */
    @GetMapping("/getGrajInfoByPay")
    public R<Object> getGrajInfoByPay(SelectInfo sif) {
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getBuyerId, sif.getOpenId())
                .eq(PayOrder::getOutTradeNo, sif.getOutTradeNo())
                .eq(PayOrder::getTradeState, "SUCCESS"));
        if (one == null) {
            return R.error(4008, "未成功支付！");
        } else { // 支付成功
            sif.setId(one.getOutTradeNo());
            sif.setKeyWord("GRSSJL&" + sif.getName() + "&" + sif.getCardNum());
            JSONObject zewubeidiao = selectInterface.zewuGrajjcxxhy(sif);
            if (zewubeidiao == null) {
                return R.error(1004, "程序出错！");
            } else if (zewubeidiao.getInteger("code") == 200) {
                if (zewubeidiao.getInteger("resultCode") == 1) {
                    return success(zewubeidiao.getJSONArray("detail").getJSONObject(0), zewubeidiao.getString("messge"));
                } else {
                    return R.error(1004, zewubeidiao.getString("messge"));
                }
            } else {
                return R.error(1004, zewubeidiao.getString("messge"));
            }
        }
    }

    /**
     * 得到个人案件基础信息核验
     *
     * @param sif
     * @return
     */
    @GetMapping("/getGrajInfo")
    public R<Object> getGrajInfo(SelectInfo sif) {
        sif.setKeyWord("GRSSJL&" + sif.getName() + "&" + sif.getCardNum());
        JSONObject zewubeidiao = selectInterface.zewuGrajjcxxhy(sif);
        if (zewubeidiao == null) {
            return R.error(1004, "程序出错！");
        } else if (zewubeidiao.getInteger("code") == 200) {
            if (zewubeidiao.getInteger("resultCode") == 1) {
                return success(zewubeidiao.getJSONArray("detail").getJSONObject(0), zewubeidiao.getString("messge"));
            } else {
                return R.error(1004, zewubeidiao.getString("messge"));
            }
        } else {
            return R.error(1004, zewubeidiao.getString("messge"));
        }
    }

    /**
     * 套餐扣费查询——法院信息详情—个人高级版
     *
     * @param sif
     * @return
     */
    @GetMapping("/getNCInfoByCombo")
    public R<JSONObject> getNCInfoByCombo(SelectInfo sif) {
        // 套餐扣费添加收费记录
//        sif.setKeyWord("FYXX&" + sif.getName() + "&" + sif.getCardNum() + "&" + sif.getMobile());
        sif.setKeyWord("FYXX&" + sif.getName() + "&" + sif.getCardNum());
        Integer debit = comboOrderService.debit(sif);
        if (debit == -1) {
            return R.error(4008, "余额不足！");
        }
        sif.setId(debit.toString());
        JSONObject zewubeidiao = selectInterface.zewuFyxxxq(sif);
        if (zewubeidiao == null) {
            return R.error(1004, "程序出错！");
        } else if ("0000".equals(zewubeidiao.getString("code")) && zewubeidiao.getJSONObject("message").getInteger("result") == 0) {
            return success(zewubeidiao.getJSONObject("message").getJSONObject("data"), zewubeidiao.getJSONObject("message").getString("resultMsg"));
        } else {
            return R.error(1004, zewubeidiao.getJSONObject("message").getString("resultMsg"));
        }
    }

    /**
     * 支付查询——全法院信息详情—个人高级版
     *
     * @param sif
     * @return
     */
    @GetMapping("/getNCInfoByPay")
    public R<Object> getNCInfoByPay(SelectInfo sif) {
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getBuyerId, sif.getOpenId())
                .eq(PayOrder::getOutTradeNo, sif.getOutTradeNo())
                .eq(PayOrder::getTradeState, "SUCCESS"));
        if (one == null) {
            return R.error(4008, "未成功支付！");
        } else { // 支付成功
            sif.setId(one.getOutTradeNo());
//            sif.setKeyWord("FYXX&" + sif.getName() + "&" + sif.getCardNum() + "&" + sif.getMobile());
            sif.setKeyWord("FYXX&" + sif.getName() + "&" + sif.getCardNum());
            JSONObject zewubeidiao = selectInterface.zewuFyxxxq(sif);
            if (zewubeidiao == null) {
                return R.error(1004, "程序出错！");
            } else if ("0000".equals(zewubeidiao.getString("code")) && zewubeidiao.getJSONObject("message").getInteger("result") == 0) {
                return success(zewubeidiao.getJSONObject("message").getJSONObject("data"), zewubeidiao.getJSONObject("message").getString("resultMsg"));
            } else {
                return R.error(1004, zewubeidiao.getJSONObject("message").getString("resultMsg"));
            }
        }
    }

    /**
     * 得到法院信息详情—个人高级版
     *
     * @param sif
     * @return
     */
    @GetMapping("/getNCInfo")
    public R<Object> getNCInf(SelectInfo sif) {
//        sif.setKeyWord("FYXX&" + sif.getName() + "&" + sif.getCardNum() + "&" + sif.getMobile());
        sif.setKeyWord("FYXX&" + sif.getName() + "&" + sif.getCardNum());
        JSONObject zewubeidiao = selectInterface.zewuFyxxxq(sif);
        if (zewubeidiao == null) {
            return R.error(1004, "程序出错！");
        } else if ("0000".equals(zewubeidiao.getString("code")) && zewubeidiao.getJSONObject("message").getInteger("result") == 0) {
            return success(zewubeidiao.getJSONObject("message").getJSONObject("data"), zewubeidiao.getJSONObject("message").getString("resultMsg"));
        } else {
            return R.error(1004, zewubeidiao.getJSONObject("message").getString("resultMsg"));
        }
    }

    /**
     * 套餐扣费查询——企业信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyInfoByCombo")
    public R<Object> getCompanyInfoByCombo(SelectInfo sif) {
        // 套餐扣费添加收费记录
        sif.setKeyWord("QYXX&" + sif.getComType() + "&" + sif.getComValue());
        Integer debit = comboOrderService.debit(sif);
        if (debit == -1) {
            return R.error(4008, "余额不足！");
        }
        sif.setId(debit.toString());
        // 查询
        SelRetInfo selRetInfo = selectInterface.getCompanyInfo(sif);
        if (selRetInfo == null) {
            return R.error(1004, "程序出错！");
        } else if (selRetInfo.isAll()) {
            return success(selRetInfo.getReturnJO());
        } else {
            // 查询出错
            return R.error(4010, selRetInfo.getMsg());
        }
    }

    /**
     * 支付查询——企业信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyInfoByPay")
    public R<Object> getCompanyInfoByPay(SelectInfo sif) {
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getBuyerId, sif.getOpenId())
                .eq(PayOrder::getOutTradeNo, sif.getOutTradeNo())
                .eq(PayOrder::getTradeState, "SUCCESS"));
        if (one == null) {
            return R.error(4008, "未成功支付！");
        } else { // 支付成功
            // 查询
            sif.setId(one.getOutTradeNo());
            sif.setKeyWord("QYXX&" + sif.getComType() + "&" + sif.getComValue());
            SelRetInfo selRetInfo = selectInterface.getCompanyInfo(sif);
            if (selRetInfo == null) {
                return R.error(1004, "程序出错！");
            } else if (selRetInfo.isAll()) {
                return success(selRetInfo.getReturnJO());
            } else {
                // 查询出错
                return R.error(4010, selRetInfo.getMsg());
            }
        }
    }

    /**
     * 得到企业信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyInfo")
    public R<Object> getCompanyInfo(SelectInfo sif) {
        sif.setKeyWord("QYXX&" + sif.getComType() + "&" + sif.getComValue());
        SelRetInfo selRetInfo = selectInterface.getCompanyInfo(sif);
        if (selRetInfo == null) {
            return R.error(1004, "程序出错！");
        } else if (selRetInfo.isAll()) {
            return R.success(selRetInfo.getReturnJO());
        } else {
            // 查询出错
            return R.error(4010, selRetInfo.getMsg());
        }
    }

    /**
     * 得到企业裁判文书信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyJudicialInfo")
    public R<Object> getCompanyJudicialInfo(SelectInfo sif) {
        sif.setKeyWord("QYCPWS&" + sif.getComType() + "&" + sif.getComValue());
        return R.success(selectInterface.getCompanyJudicialInfo(sif, sif.getPageNum()));
    }

    /**
     * 套餐扣费查询——企业任职信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyPostInfoByCombo")
    public R<Object> getCompanyPostInfoByCombo(SelectInfo sif) {
        // 套餐扣费添加收费记录
        sif.setKeyWord("QYRZ&" + sif.getName() + "&" + sif.getCardNum());
        Integer debit = comboOrderService.debit(sif);
        if (debit == -1) {
            return R.error(4008, "余额不足！");
        }
        sif.setId(debit.toString());
        // 查询
        SelRetInfo selRetInfo = selectInterface.getCompanyPostInfo(sif);
        if (selRetInfo.isAll()) {
            return success(selRetInfo.getReturnJO());
        } else {
            // 查询出错
            return R.error(4010, selRetInfo.getMsg());
        }
    }

    /**
     * 支付查询——企业任职信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyPostInfoByPay")
    public R<Object> getCompanyPostInfoByPay(SelectInfo sif) {
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getBuyerId, sif.getOpenId())
                .eq(PayOrder::getOutTradeNo, sif.getOutTradeNo())
                .eq(PayOrder::getTradeState, "SUCCESS"));
        if (one == null) {
            return R.error(4008, "未成功支付！");
        } else {
            // 支付成功;
            sif.setId(sif.getOutTradeNo());
            sif.setKeyWord("QYRZ&" + sif.getName() + "&" + sif.getCardNum());
            // 查询
            SelRetInfo selRetInfo = selectInterface.getCompanyPostInfo(sif);
            if (selRetInfo.isAll()) {
                return success(selRetInfo.getReturnJO());
            } else {
                // 查询出错
                return R.error(4010, selRetInfo.getMsg());
            }
        }
    }

    /**
     * 得到企业任职信息
     *
     * @param sif
     * @return
     */
    @GetMapping("/getCompanyPostInf")
    public R<Object> getCompanyPostInf(SelectInfo sif) {
        sif.setKeyWord("QYRZ&" + sif.getName() + "&" + sif.getCardNum());
        SelRetInfo selRetInfo = selectInterface.getCompanyPostInfo(sif);
        if (!selRetInfo.isAll()) {
            return R.error(4010, selRetInfo.getMsg());
        } else {
            // 查询出错
            return R.success(selRetInfo.getReturnJO());
        }
    }

}
